package com.foreknow.config;

import com.foreknow.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * 跨域请求支持/token拦截
 * tip:只能写在一个配置类里
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

  private TokenInterceptor tokenInterceptor;

  //构造方法
  public WebConfiguration(TokenInterceptor tokenInterceptor){
    this.tokenInterceptor = tokenInterceptor;
  }

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
      .allowCredentials(true)
      .allowedHeaders("*")
      .allowedMethods("*")
      .allowedOrigins("*");
  }

  @Override
  public void configureAsyncSupport(AsyncSupportConfigurer configurer){
    configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
    configurer.setDefaultTimeout(30000);
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry){
    List<String> excludePath = new ArrayList<>();
    //排除拦截
    /**
     * 用户
     */
    excludePath.add("/user/register");  //登录
    excludePath.add("/user/login");     //注册
    excludePath.add("/user/findUserByPage"); //测试查询用户
    excludePath.add("/user/insertUser"); //测试新增用户
    excludePath.add("/user/deleteUser"); //测试删除用户
    excludePath.add("/user/selectUser"); //修改之前查询用户
    excludePath.add("/user/updateUser"); //测试修改用户
    excludePath.add("/user/deleteList"); //批量删除用户
    /**
     * 科室
     */
    excludePath.add("/department/findDepartmentByPage"); //测试查询科室
    excludePath.add("/department/insertDepartment"); //测试新增科室
    excludePath.add("/department/deleteDepartment"); //测试删除科室
    excludePath.add("/department/selectDepartment"); //修改之前查询科室
    excludePath.add("/department/updateDepartment"); //测试修改科室
    excludePath.add("/department/deleteDepartmentList"); //批量删除科室
    excludePath.add("/static/**");  //静态资源
    excludePath.add("/assets/**");  //静态资源
    /**
     * 结算类别
     */
    excludePath.add("/settlecategory/findSettlecategoryByPage");//分页获取列表
    excludePath.add("/settlecategory/deleteSettlecategoryById");//删除结算类别
    excludePath.add("/settlecategory/add");//添加结算类别
    excludePath.add("/settlecategory/select");//修改前选择结算类别
    excludePath.add("/settlecategory/update");//修改结算类别
    excludePath.add("/settlecategory/deleteList");//批量删除

    /**
     * 挂号级别管理
     */
    excludePath.add("/reg/findLevelByPage");
    excludePath.add("/reg/addLevel");
    excludePath.add("/reg/selectLevelById");
    excludePath.add("/reg/updateLevel");
    excludePath.add("/reg/deleteLevel");
    excludePath.add("/reg/deleteMany");

    /**
     * 疾病分类
     */
    excludePath.add("/disease/findDiseaseByPage"); //疾病管理
    excludePath.add("/disease/findDisecategoryByPage"); //疾病管理
    excludePath.add("/disease/addCategory"); //添加疾病分类
    excludePath.add("/disease/deleteCategory"); //删除疾病分类
    excludePath.add("/disease/selectCategoryOne");  //查询一个疾病分类
    excludePath.add("/disease/updatetCategory");  //更新疾病分类

    /**
     * 疾病
     */
    excludePath.add("/disease/updatetDisease");   //修改疾病信息
    excludePath.add("/disease/addDisease");   //添加疾病信息
    excludePath.add("/disease/deleteDisease");   //删除疾病信息
    excludePath.add("/disease/selectDiseaseOne");   //查询一条疾病信息
    excludePath.add("/disease/findAllDiseaseByPage");   //查询一条疾病信息

    /**
     * 常数类别管理
     */
    excludePath.add("/constanttype/findConstanttypeByPage");   //获取常数类别列表和查询常数类别
    excludePath.add("/constanttype/addConstanttype");  //添加常数类别
    excludePath.add("/constanttype/selectConstanttype");  //修改之前查询常数类别
    excludePath.add("/constanttype/updateConstanttype");  //修改常数类别
    excludePath.add("/constanttype/deleteConstanttype");  //删除常数类别

    /**
     * 常数管理
     */
    excludePath.add("/constantitem/findConstantitemByPage");  //获取常数列表和查询常数
    excludePath.add("/constantitem/addConstantitem");  //添加常数项
    excludePath.add("/constantitem/deleteConstantitem");  //删除常数项
    excludePath.add("/constantitem/selectConstantitem");  //修改之前查询常数项
    excludePath.add("/constantitem/updatetConstantitem");  //修改常数项
    excludePath.add("/constantitem/findConstanttypeitemByPage");  //获取常数类别的详情

    /**
     * 非药品收费
     */
    excludePath.add("/fmeditem/updateFmeditem");   //修改非药品收费信息
    excludePath.add("/fmeditem/addFmeditem");   //添加非药品收费信息
    excludePath.add("/fmeditem/deleteFmeditem");   //删除非药品收费信息
    excludePath.add("/fmeditem/selectFmeditemOne");   //查询一条非药品收费信息
    excludePath.add("/fmeditem/findFmeditemByPage");   //查询非药品收费信息
    excludePath.add("/fmeditem/selectDeptName");   //查询非药品收费相关部门信息
    excludePath.add("/fmeditem/selectExpName");   //查询非药品收费相关分类信息

    /**
     * 排班
     */
    excludePath.add("/doctor/doctorduty");//分页查询排班人员
    excludePath.add("/doctor/departments");//查询所有科室
    excludePath.add("/doctor/addrules");//查询所有科室
    excludePath.add("/doctor/addrulebyrealname");//保存规则
    excludePath.add("/doctor/generateplan");//选取规则生成排班计划

    registry.addInterceptor(tokenInterceptor)
      .addPathPatterns("/**")
      .excludePathPatterns(excludePath);
    WebMvcConfigurer.super.addInterceptors(registry);
  }
}
